document.on('domready', function () {
    var $tableAdd = $('.table_add > tbody');
    //显示所有用户信息
    new Request('http://111.207.228.245/itsmservice/user/UserAction.ashx?method=find')
        .on('complete', function (e) {
            var dataUser = JSON.parse(e.text);
            dataUser.forEach(function ($item) {
                var $row = $('<tr><td>'+ $item.username +'</td><td>'+ $item.pwd +'</td><td>'+ $item.name +'</td><td>'+ $item.mobile +'</td><td>'+ $item.workphone +'</td><td>'+ $item.fj +'</td><td>'+ $item.email +'</td><td>'+ $item.qq +'</td><td>'+ $item.wx +'</td><td>'+ $item.role +'</td><td>'+ $item.equips +'</td><td>'+ $item.workdepar +'</td><td>'+ $item.workcode +'</td><td>'+ $item.operator +'</td><td><img src="images/pencil.png" class="btn_edit" title="编辑" alt="编辑" style="cursor: pointer;"></td></tr>');
                $row.insertTo($tableAdd);
            });
            //修改用户信息
            var editList = $tableAdd.findAll('.btn_edit');
            var $btnUserUpdate = $('#btn_user_update');
            editList.forEach(function($itemUser) {
                var $dialogEditTitle = $('#dialog_add_user > h2.edit_user');
                var $dialogAddTitle = $('#dialog_add_user > h2.add_user');
                var $trUser = $itemUser.getParent().getParent();
                var trUsername = $trUser.cells[0].innerText;
                var trPwd = $trUser.cells[1].innerText;
                var trName = $trUser.cells[2].innerText;
                var trMobile = $trUser.cells[3].innerText;
                var trEmail = $trUser.cells[6].innerText;
                var trRole = $trUser.cells[9].innerText;
                var trEquips = $trUser.cells[10].innerText;
                var trQQ = $trUser.cells[7].innerText;
                var trWorkphone = $trUser.cells[4].innerText;
                var trWx = $trUser.cells[8].innerText;
                var trFj = $trUser.cells[5].innerText;
                var trWorkDepar = $trUser.cells[11].innerText;
                var trWorkcode = $trUser.cells[12].innerText;
                var trOperator = $trUser.cells[13].innerText;
                $itemUser.on('click', function () {
                    $dialogAdd.open();
                    $dialogAddTitle.setStyle('display', 'none');
                    $dialogEditTitle.setStyle('display', 'block');
                    $btnAdd.setStyle('display', 'none');
                    $btnUserUpdate.setStyle('display', 'block');
                    $inpUserName.value = trUsername;
                    $inpPwd.value = trPwd;
                    $inpName.value = trName;
                    $inpMobile.value = trMobile;
                    $inpEmail.value = trEmail;
                    $inpRole.value = trRole;
                    console.log("[value='" + trEquips + "']")
                    console.log($("[value='" + trEquips + "']", selEquips))
                    $("[value='" + trEquips + "']", selEquips).selected = true;
                    $inpQQ.value = trQQ;
                    $inpWorkPhone.value = trWorkphone;
                    $inpWx.value = trWx;
                    $inpFj.value = trFj;
                    $inpWorkDepar.value = trWorkDepar;
                    $inpWorkCode.value = trWorkcode;
                    $inpOperator.value = trOperator;
                    $btnUserUpdate.on('click', function () {
                        new Request('http://111.207.228.245/itsmservice/user/UserAction.ashx?method=update')
                            .on('complete', function (e) {
                                console.log(e.text);
                                if (e.text === 'true') {
                                    alert('已成功修改用戶信息！');
                                }
                                $trUser.remove();
                                $trUser = $('<tr><td>'+ $inpUserName.value +'</td><td>'+ $inpPwd.value +'</td><td>'+ $inpName.value +'</td><td>'+ $inpMobile.value +'</td><td>'+ $inpWorkPhone.value +'</td><td>'+ $inpFj.value +'</td><td>'+ $inpEmail.value +'</td><td>'+ $inpQQ.value +'</td><td>'+ $inpWx.value +'</td><td>'+ $inpRole.value +'</td><td>'+ selEquips.options[selEquips.selectedIndex].value +'</td><td>'+ $inpWorkDepar.value +'</td><td>'+ $inpWorkCode.value +'</td><td>'+ $inpOperator.value +'</td><td><img src="images/pencil.png" class="btn_edit" title="编辑" alt="编辑" style="cursor: pointer;"></td></tr>');
                                $trUser.insertTo($tableAdd);
                            })
                            .send({'usercode':3062, 'passwd': $inpPwd.value, 'name': $inpName.value, 'mobile': $inpMobile.value, 'email': $inpEmail.value, 'role': $inpRole.value, 'equips': selEquips.options[selEquips.selectedIndex].value, 'qq': $inpQQ.value, 'wx': $inpWx.value});
                    });
                    $dialogAdd.close();
                });
            });
        })
        .send();
    //用户管理-添加新用户
    var $btnAddUser = $('#btn_add_user');  //页面添加按钮
    var $btnAdd = $('#btn_user_assure');   //弹窗确定按钮
    var $btnCancel = $('#btn_cancel');     //弹窗取消按钮
    var $dialogAdd = $('#dialog_add_user');     //添加弹窗
    $btnAddUser.on('click', function () {
        $dialogAdd.open();
    });
    $btnCancel.on('click', function () {
        $dialogAdd.close();
    });
    var tips = $('#dialog_add_user > ul').findAll('li > .tip');
    var $inpUserName = $('#inp_username');
    var $inpPwd = $('#inp_pwd');
    var $inpName = $('#inp_name');
    var $inpMobile = $('#inp_mobile');
    var $inpEmail = $('#inp_email');
    var $inpRole = $('#inp_role');
    var selEquips = $('#sel_equips');
    var $inpQQ = $('#inp_qq');
    var $inpWx = $('#inp_wx');
    var $inpWorkPhone = $('#inp_workphone');
    var $inpFj = $('#inp_fj');
    var $inpWorkDepar = $('#inp_workdepar');
    var $inpWorkCode = $('#inp_workcode');
    var $inpOperator = $('#inp_operater');
    //验证用户名是否存在
    $inpUserName.on('change', function () {
        new Request('http://111.207.228.245/itsmservice/user/UserAction.ashx?method=isUsernameExist')
            .on('complete', function (e) {
                var dataName = JSON.parse(e.text);
                if (dataName) {
                    tips[0].setStyle('display', 'inline-block');
                }
                else if (!dataName) {
                    tips[0].setStyle('display', 'none');
                }
            })
            .send({ 'username': $inpUserName.value });
    });
    var username = $inpUserName.value;
    var pwd = $inpPwd.value;
    var name = $inpName.value;
    var mobile = $inpMobile.value;
    var email = $inpEmail.value;
    var role = $inpRole.value;
    var equips = selEquips.options[selEquips.selectedIndex].value;
    var qq = $inpQQ.value;
    var workphone = $inpWorkPhone.value;
    var wx = $inpWx.value;
    var fj = $inpFj.value;
    var workdepar = $inpWorkDepar.value;
    var workcode = $inpWorkCode.value;
    var operator = $inpOperator.value;
    //添加新用户并在列表中显示
    $btnAdd.on('click', function () {
        new Request('http://111.207.228.245/itsmservice/user/UserAction.ashx?method=register')
            .on('complete', function (e) {
                if (username && pwd && name && mobile && email && role && equips) {
                    if (e.text === "True" || "true") {
                        alert('新用户添加成功！');
                    }
                    $dialogAdd.close();
                } else {
                    alert('对不起，请填写正确的用户信息！');
                }
            })
            .send({ 'username': username, 'passwd': pwd, 'name': name, 'mobile': mobile, 'email': email, 'role': role, 'equips': equips});
        var $row = $('<tr><td>'+ username +'</td><td>'+ pwd +'</td><td>'+ name +'</td><td>'+ mobile +'</td><td>'+ workphone +'</td><td>'+ fj +'</td><td>'+ email +'</td><td>'+ qq +'</td><td>'+ wx +'</td><td>'+ role +'</td><td>'+ equips +'</td><td>'+ workdepar +'</td><td>'+ workcode +'</td><td>'+ operator +'</td><td><img src="images/pencil.png" class="btn_edit" title="编辑" alt="编辑" style="cursor: pointer;"></td></tr>');
        $row.insertTo($tableAdd);
    });
    //用户分组信息展示
    var groupList = $('.ul_group').findAll('i');
    new Request('http://111.207.228.245/itsmservice/manager/RoleManagerAction.ashx?method=get')
        .on('complete', function (e) {
            var dataCurrentGroup = JSON.parse(e.text);
            groupList[0].innerText = dataCurrentGroup.rolecode;
            groupList[1].innerText = dataCurrentGroup.rolename;
            groupList[2].innerText = dataCurrentGroup.juris;
            groupList[3].innerText = dataCurrentGroup.escry;
            groupList[4].innerText = dataCurrentGroup.operator;
            groupList[5].innerText = dataCurrentGroup.uptime;

        })
        .send({'rolecode': 3071});
    //所有用戶分組信息列表
    var $tableGroup = $('.table_group > tbody');
    var $dialogDel = $('#dialog_del');
    var $btnDelAssure = $('#btn_del_assure');
    var $btnDelCancel = $('#btn_del_cancel');
    new Request('http://111.207.228.245/itsmservice/manager/RoleManagerAction.ashx?method=find')
        .on('complete', function (e) {
            var dataGroup = JSON.parse(e.text);
            dataGroup.forEach(function ($itemGroup) {
                var $rowGroup = $('<tr><td>'+ $itemGroup.rolecode +'</td><td>'+ $itemGroup.rolename +'</td><td>'+ $itemGroup.juris +'</td><td>'+ $itemGroup.operator +'</td><td>'+ $itemGroup.uptime +'</td><td>'+ $itemGroup.desrc +'</td><td><img src="images/pencil.png" title="编辑" alt="编辑" style="cursor: pointer;"><img src="images/cross.png" class="btn_del" title="删除" alt="删除" style="margin-left: 5px; cursor: pointer;"></td></tr>');
                $rowGroup.insertTo($tableGroup);
                var btnDel = $tableGroup.findAll('.btn_del');
                //删除用户分组信息
                btnDel.forEach(function ($tr) {
                    $tr.on('click', function () {
                        $dialogDel.open();
                        $btnDelAssure.on('click', function () {
                            $tr.getParent().getParent().remove();
                            $dialogDel.close();
                        });
                        $btnDelCancel.on('click', function () {
                            $dialogDel.close();
                        });
                    });
                    new Request('http://111.207.228.245/itsmservice/manager/RoleManagerAction.ashx?method=delete')
                        .on('complete', function (e) {
                            var dataDel = JSON.parse(e.text);
                        })
                        .send({'rolecode': $itemGroup.rolecode});
                });
            });
        })
        .send();
    //添加新用户分组
    var $btnAddGroup = $('#btn_add_group');
    var $dialogAddGroup = $('#dialog_add_group');
    var $btnGroupAssure = $('#btn_group_assure');   //弹窗中确定按钮
    var $btnGroupCancel = $('#btn_group_cancel');   //弹窗中取消按钮
    var $inpRoleName = $('#inp_rolename');
    var $selJuris = $('#sel_juris');
    var $inpDescry = $('#inp_descry');
    var $inpGroupOperator = $('#inp_group_operator');
    $btnAddGroup.on('click', function () {
        $dialogAddGroup.open();
    });
    $btnGroupCancel.on('click', function () {
        $dialogAddGroup.close();
    });
    $btnGroupAssure.on('click', function() {
        var rolename = $inpRoleName.value;
        var juris = $selJuris.options[$selJuris.selectedIndex].value;
        var descry = $inpDescry.value;
        var groupoperator = $inpGroupOperator.value;
        new Request('http://111.207.228.245/itsmservice/manager/RoleManagerAction.ashx?method=add')
            .on('complete', function(e) {
                var dataNewGroup = JSON.parse(e.text);
                var $rowNewGroup = $('<tr><td>'+ dataNewGroup.rolecode +'</td><td>' + rolename + '</td><td>' + juris + '</td><td>' + descry + '</td><td>' + groupoperator + '</td><td><img src="images/pencil.png" title="编辑" alt="编辑" style="cursor: pointer;"><img src="images/cross.png" class="btn_del" title="删除" alt="删除" style="margin-left: 5px; cursor: pointer;"></td></tr>');
                $rowNewGroup.insertTo($tableGroup);
            })
            .send({'rolename': rolename, 'juris': juris});
        if (rolename && juris) {
            $dialogAddGroup.close();
        } else {
            alert('对不起，用户分组填写错误！');
        }
    });
});
